package com.nariano.webapp;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.nariano.server.domain.entities.User;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.services.userservice.UserService;
import com.nariano.server.utils.spring.SpringFactory;

/**
 * Homepage
 */
public class HomePage extends WebPage {

	private static final long serialVersionUID = 1L;

	
    /**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters Page parameters
	 */
    public HomePage(final PageParameters parameters) {

        final CityService cityService = SpringFactory.getBean("cityService", CityService.class);
        
        IModel usersModel = new LoadableDetachableModel() {

            @Override
            protected Object load() {
                UserService userService = SpringFactory.getBean("userService", UserService.class);
                return userService.getAllUsers();
            }
            
        };
        
        // Add the simplest type of label
        add(new Label("message", "If you see this message wicket is properly configured and running"));
        add(new ListView("users", usersModel) {

            @Override
            protected void populateItem(ListItem item) {
                final User user = (User) item.getModelObject();
                
                IModel citiesModel = new LoadableDetachableModel() {

                    @Override
                    protected Object load() {
                        return Integer.valueOf(cityService.getCities(user).size());
                    }
                    
                };
                

                item.add(new Label("username", new PropertyModel(user, "userName")));
                item.add(new Label("email", new PropertyModel(user, "email")));
                item.add(new Label("cities", citiesModel));
                item.add(new Link("edit", item.getModel()){

                    @Override
                    public void onClick() {
                        throw new UnsupportedOperationException();
                        //setResponsePage(new EditPlayerPage(user));
                    }});
            }});
        
        add(new Link("add", null){

            @Override
            public void onClick() {
                throw new UnsupportedOperationException(); //(setResponsePage(new EditPlayerPage(new User()));
       }});


            
    }

}
